const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const helmet = require('helmet');

const { swaggerUi, swaggerSpec } = require('./components/swagger');
const authRouter = require('./routes/auth');
const supplierRoutes = require('./routes/supplierRoutes');
const treeMenuRoutes = require("./routes/treeMenuRoutes");
const projectRoutes = require("./routes/projectRoutes");
const warehouseRoutes = require("./routes/warehouseRoutes");
const permissionRouter = require('./routes/permissionRoutes');
const authMiddleware = require('./middleware/authMiddleware'); //JWT鉴权
const requirePermission = require('./middleware/roleMiddleware');//角色鉴权


const app = express();
app.use(cors({
    origin: '*',
    allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(cors());
app.use(helmet());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


app.use('/api-docs', [swaggerUi.serve, swaggerUi.setup(swaggerSpec)]);
app.use('/api/auth', authRouter);
app.use('/api/suppliers', supplierRoutes);
app.use('/api/treeMenu', treeMenuRoutes);
app.use('/api/projects', projectRoutes);
app.use('/api/warehouses', warehouseRoutes);
app.use('/api/permissions', permissionRouter);
app.use('/api/projects', projectRoutes);
app.get('/api/suppliers', authMiddleware, requirePermission(), (req, res) => {
    res.json({ msg: '访问供应商列表成功' });
});

module.exports = app;
